//
//  ZZMainNewsCollectionView.m
//  ZZNews
//
//  Created by 周英培 on 16/7/27.
//  Copyright © 2016年 周英培. All rights reserved.
//

#define MainNewsCollectionViewCell @"MainNewsCollectionViewCell"
#define KWIDTH [UIScreen mainScreen].bounds.size.width

#import "ZZMainNewsCollectionView.h"
#import "ZZMainNewsCollectionViewCell.h"
@interface ZZMainNewsCollectionView ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong) NSMutableArray *channels;

@end

@implementation ZZMainNewsCollectionView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {

    self = [super initWithFrame:frame collectionViewLayout:layout];
    
    if (self) {
        
        self.dataSource = self;
        
        self.delegate = self;
        
        self.pagingEnabled = YES;
        
        self.showsVerticalScrollIndicator = NO;
        
        self.showsHorizontalScrollIndicator = NO;
        
        self.bounces = NO;
        
        //MARK:注册cell
        [self registerClass:[ZZMainNewsCollectionViewCell class] forCellWithReuseIdentifier:MainNewsCollectionViewCell];
        
        //MARK:注册通知，接收新闻频道栏的数据
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNewsChannelDataWithNotify:) name:@"NewsChannelData" object:nil];
        
        //MARK:z注册通知，接收新闻频道栏滚动点击的位置，使新闻主界面加载相应的新闻数据
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNewsChannelIndexPathWithNotify:) name:@"NewsChannelIndexPath" object:nil];
        
    }
    
    return self;

}

///注册通知的接收方法，接收新闻频道栏滚动点击的位置，使新闻主界面加载相应的新闻数据
- (void)getNewsChannelIndexPathWithNotify:(NSNotification *)notify {
    
    NSIndexPath *indexPath = notify.object;
    
    [self scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    
}


/// 接收新闻频道栏数据的通知方法
- (void)getNewsChannelDataWithNotify:(NSNotification *)notify {

    self.channels = notify.object;
    
    [self reloadData];

}

/// 销毁通知
- (void)dealloc {

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.channels.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    ZZMainNewsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MainNewsCollectionViewCell forIndexPath:indexPath];
    
    ZZNewsChannelModel *newsChannelModel = self.channels[indexPath.item];
    
    // 将模型数据传递给cell
    cell.newsChannelModel = newsChannelModel;
    
    return cell;

}

#pragma mark - UIScrollViewDelegate
/// 滚动或者减速完全停止后的方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    //MARK:发送通知，将新闻主界面滚动或者减速完全停止后的位置发送给新闻频道栏，让新闻频道栏滚动到相应的位置
    NSInteger index = scrollView.contentOffset.x / KWIDTH;
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MainNewsIndexPath" object:indexPath];

}

@end
